const connection = require("../app/database");

class PermissionService {
  // async checkMoment(momentId, userId) {
  //   const statement = "SELECT * FROM moment WHERE id = ? AND user_id = ?;";

  //   const [result] = await connection.execute(statement, [momentId, userId]);
  //   return !!result.length;
  // }
  /**
   * @description 检查资源是否存在
   * @param {String} resourceName 表名
   * @param {Number} resourceId 资源id
   */
  async searchResource(resourceName, resourceId){
    const statement = `SELECT * FROM ${resourceName} WHERE id = ? `;

    const [result] = await connection.execute(statement, [resourceId]);
    return !!result.length;
  }
  /**
   * @description 检查资源是否与用户是否匹配-权限
   * @param {String} resourceName 表名
   * @param {Number} resourceId 资源id
   * @param {Number} userId 用户id
   */
  async checkResouce(resourceName, resourceId, userId) {
    const statement = `SELECT * FROM ${resourceName} WHERE id = ? AND user_id = ?;`;

    const [result] = await connection.execute(statement, [resourceId, userId]);
    return !!result.length;
  }
}

module.exports = new PermissionService();